package com.chookss.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.StaticClass;
import com.chookss.home.adapter.NavigationAdapter;
import com.chookss.home.adapter.NewsListsAdapter;
import com.chookss.home.entity.HomeBannerEntity;
import com.chookss.home.entity.NavigationEntity;
import com.chookss.home.entity.NewsEntiry;
import com.chookss.home.entity.UnReadMessageEntity;
import com.chookss.home.interview.InterviewRecordsActivity;
import com.chookss.home.other.ScanActivity;
import com.chookss.home.other.WxBindActivity;
import com.chookss.home.search.SearchRecordActivity;
import com.chookss.home.workbench.WorksActivity;
import com.chookss.mine.entity.MineInfoEntity;
import com.chookss.mine.set.UserInfosActivity;
import com.chookss.mine.system.MyIntegralActivity;
import com.chookss.mine.system.MyPraiseActivity;
import com.chookss.mvp.presenter.HomePagePresenter;
import com.chookss.mvp.view.IHomepageView;
import com.chookss.push.PushUtils;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.FileUtilss;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.PermissionSpUtils;
import com.chookss.tools.Utils;
import com.chookss.view.CircleImageView;
import com.chookss.view.NoticeDialog;
import com.chookss.view.RecycleViewDivider;
import com.chookss.view.listener.ShareDialogListener;
import com.chookss.view.consecutivescroller.ConsecutiveScrollerLayout;
import com.google.zxing.integration.android.IntentIntegrator;
import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.components.SimpleImmersionFragment;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

public class HomeFragmentss extends SimpleImmersionFragment implements IHomepageView {

    @BindView(R.id.rcvNavigation)
    RecyclerView rcvNavigation;
    @BindView(R.id.titleFlipper2)
    ViewFlipper titleFlipper2;
    @BindView(R.id.titleFlipper)
    ViewFlipper titleFlipper;
    @BindView(R.id.llSearch)
    LinearLayout llSearch;
    @BindView(R.id.llSearch2)
    LinearLayout llSearch2;
    @BindView(R.id.llName)
    LinearLayout llName;
    @BindView(R.id.llSearchConent)
    LinearLayout llSearchConent;
    @BindView(R.id.ivHead)
    CircleImageView ivHead;
    @BindView(R.id.ivHead2)
    CircleImageView ivHead2;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvLevel)
    TextView tvLevel;
    @BindView(R.id.tvScore)
    TextView tvScore;
    @BindView(R.id.ivLevel)
    ImageView ivLevel;
    @BindView(R.id.tvUpgradeScore)
    TextView tvUpgradeScore;
    @BindView(R.id.tvNextLevel)
    TextView tvNextLevel;
    @BindView(R.id.tvNextLevel2)
    TextView tvNextLevel2;
    @BindView(R.id.tvCompany)
    TextView tvCompany;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.rlTop)
    RelativeLayout rlTop;
    @BindView(R.id.viewFlipper)
    ViewFlipper viewFlipper;
    @BindView(R.id.llViewFlipper)
    LinearLayout llViewFlipper;
    @BindView(R.id.tvTodayTask)
    TextView tvTodayTask;
    @BindView(R.id.llTodayTask)
    LinearLayout llTodayTask;
    @BindView(R.id.tvInterviewTask)
    TextView tvInterviewTask;
    @BindView(R.id.llInterviewTask)
    LinearLayout llInterviewTask;
    @BindView(R.id.llBindTask)
    LinearLayout llBindTask;
    @BindView(R.id.tvJob)
    TextView tvJob;
    @BindView(R.id.llLevel)
    LinearLayout llLevel;
    @BindView(R.id.llJob)
    LinearLayout llJob;
    @BindView(R.id.llTask)
    LinearLayout llTask;
    @BindView(R.id.homeBanner)
    Banner homeBanner;
    @BindView(R.id.spaceNews)
    View spaceNews;
    @BindView(R.id.llNews)
    LinearLayout llNews;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smrRf)
    SmartRefreshLayout smrRf;
    @BindView(R.id.llBanner)
    LinearLayout llBanner;
    @BindView(R.id.consecutiveScrollerLayout)
    ConsecutiveScrollerLayout consecutiveScrollerLayout;
    private Context mContext;
    private Activity mActivity;
    Unbinder unbinder;
    private int pageNum = 1;
    private boolean isEnd = false;//是否到底
    private HomePagePresenter homePagePresenter;
    private NewsListsAdapter adapter;
    private View emptyView;
    private TextView tvEmpty, tvEmpty2;
    private ImageView ivEmpty;//空布局
    private List<NewsEntiry> newsList = new ArrayList<>();
    private List<String> flipper = new ArrayList<>();
    private List<String> flipper2 = new ArrayList<>();
    private NavigationAdapter navigationAdapter;
    private ArrayList<NavigationEntity> navigationEntityList = new ArrayList<>();
    private int netNum = 0;//第几次请求个人信息，第一次的话延迟200ms

    public HomeFragmentss() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (FragmentActivity) activity;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_homess, container, false);
        unbinder = ButterKnife.bind(this, view);
        homePagePresenter = new HomePagePresenter(mActivity, this);
        init();
        EventBus.getDefault().register(this);
        return view;
    }

    private void init() {
        //顶部状态栏 处理
        int statusBarHeight = Utils.getStatusBarHeight(mContext);
        ViewGroup.LayoutParams llSearchParams = (ViewGroup.LayoutParams) llSearch.getLayoutParams();
        llSearchParams.height = statusBarHeight + Utils.dip2px(mContext, 44);
        llSearch.setLayoutParams(llSearchParams);
        llSearch.setPadding(0, statusBarHeight, 0, 0);
        ViewGroup.LayoutParams rlTopParams = (ViewGroup.LayoutParams) rlTop.getLayoutParams();
        rlTopParams.height = Utils.dip2px(mContext, 146) + statusBarHeight;
        rlTop.setPadding(0, statusBarHeight, 0, 0);
        rlTop.setLayoutParams(rlTopParams);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager
                .VERTICAL, false));
        recyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5f), getResources().getColor(R.color.line_gray)));
        adapter = new NewsListsAdapter(R.layout.item_news, mContext, newsList);
        recyclerView.setAdapter(adapter);
        emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_common_empty, recyclerView, false);
        ivEmpty = emptyView.findViewById(R.id.ivEmpty);
        tvEmpty = emptyView.findViewById(R.id.tvEmpty);
        tvEmpty2 = emptyView.findViewById(R.id.tvEmpty2);
        tvEmpty.setText("还没有动态内容");
        tvEmpty2.setText("快去发布吧");
        tvEmpty2.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams emptyParams = (LinearLayout.LayoutParams) ivEmpty.getLayoutParams();
        emptyParams.setMargins(0, Utils.dip2px(mContext, 20), 0, 0);
        ivEmpty.setLayoutParams(emptyParams);
        LinearLayout.LayoutParams tvEmpty2Params = (LinearLayout.LayoutParams) tvEmpty2.getLayoutParams();
        tvEmpty2Params.setMargins(0, 0, 0, Utils.dip2px(mContext, 140));
        tvEmpty2.setLayoutParams(tvEmpty2Params);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        smrRf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                if (isEnd) {//has next page
                    MyToast.show("没有更多数据了");
                    loadEnd();
                } else {
                    pageNum++;
                    homePagePresenter.getNewsList(pageNum);
                }
            }
        });
        smrRf.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                pageNum = 1;
                isEnd = false;
                homePagePresenter.getNewsList(pageNum);
                homePagePresenter.getMyWorkList();
                homePagePresenter.getAllBanner();
                homePagePresenter.getNotReadMessageByEmployee();
            }
        });

        RelativeLayout.LayoutParams ivHeadParams = (RelativeLayout.LayoutParams) ivHead.getLayoutParams();
        LinearLayout.LayoutParams llSearchConentParams = (LinearLayout.LayoutParams) llSearchConent.getLayoutParams();

        consecutiveScrollerLayout.setOnVerticalScrollChangeListener(new ConsecutiveScrollerLayout.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollY, int oldScrollY, int scrollState) {
                if (ivHead == null) {
                    return;
                }
                if (scrollY > Utils.dip2px(mContext, 51)) {
                    ivHeadParams.height = Utils.dip2px(mContext, 22);
                    ivHeadParams.width = Utils.dip2px(mContext, 22);
                    llSearchConentParams.width = Utils.getScreenWidthPixel(mContext) - Utils.dip2px(mContext, 91);
                    ivHead.setLayoutParams(ivHeadParams);
                    llSearchConent.setLayoutParams(llSearchConentParams);
                    ivHead2.setVisibility(View.VISIBLE);
                    ivHead.setVisibility(View.GONE);
                    llSearch.setBackgroundResource(R.drawable.home_title_bg);
                } else {
                    float ratio = (float) scrollY / Utils.dip2px(mContext, 51);
                    ivHeadParams.height = (int) (Utils.dip2px(mContext, 44) / (1 + ratio));
                    ivHeadParams.width = (int) (Utils.dip2px(mContext, 44) / (1 + ratio));
                    llSearchConentParams.width = (int) (Utils.getScreenWidthPixel(mContext) - Utils.dip2px(mContext, 57) - Utils.dip2px(mContext, 34) * ratio);
                    ivHead.setLayoutParams(ivHeadParams);
                    llSearchConent.setLayoutParams(llSearchConentParams);
                    ivHead2.setVisibility(View.GONE);
                    ivHead.setVisibility(View.VISIBLE);
                    llSearch.setBackgroundResource(R.drawable.bg_transparent);
                }
                if (scrollY > 0) {
                    llSearch2.setVisibility(View.INVISIBLE);
                    llSearch.setVisibility(View.VISIBLE);
                } else {
                    llSearch2.setVisibility(View.VISIBLE);
                    llSearch.setVisibility(View.GONE);
                }
                if (scrollY > Utils.dip2px(mContext, 16)) {
                    float ratio = (float) (scrollY - Utils.dip2px(mContext, 16)) / Utils.dip2px(mContext, 22);
                    if (ratio > 1) {
                        ratio = (float) 1.0;
                    }
                    llName.setAlpha((float) 1.0 - ratio);
                } else {
                    llName.setAlpha((float) 1.0);
                }
            }
        });
        //导航
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 4);
        rcvNavigation.setLayoutManager(gridLayoutManager);
        navigationAdapter = new NavigationAdapter(R.layout.item_home_navigation, mContext, navigationEntityList);
        rcvNavigation.setAdapter(navigationAdapter);
        //判断是否有推送缓存
        if(!Utils.isNull(new ShareUtils().getString(mContext, StaticClass.PUSH_DATA, ""))){
            PushUtils.getPushData(mContext);
        }
        //只要启动调用
        homePagePresenter.checkLogin();
        homePagePresenter.getCostConfig();
        //水印只有启动和切换账号才获取
        homePagePresenter.getWatermarkList();
        homePagePresenter.getOSSkey();
        homePagePresenter.getNavigations();
        homePagePresenter.getCatalogData();
        smrRf.autoRefresh();
        getNOTIFICATION();
        initFlipper();
    }

    //顶部轮播
    private void initFlipper() {
        flipper.add("请输入搜索关键词");
        flipper.add("复制题目题干，可查看题目详情");
        flipper.add("试试搜索提供者、发布人、考生等人员姓名");
        flipper.add("支持搜索课程视频及资料");
        flipper.add("请输入面试或试卷名称");
        flipper2.addAll(flipper);
        titleFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext,
                R.anim.slide_bottom_in));
        titleFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,
                R.anim.slide_up_out));
        titleFlipper.startFlipping();
        titleFlipper2.setInAnimation(AnimationUtils.loadAnimation(mContext,
                R.anim.slide_bottom_in));
        titleFlipper2.setOutAnimation(AnimationUtils.loadAnimation(mContext,
                R.anim.slide_up_out));
        titleFlipper2.startFlipping();
        try {
            for (int i = 0; i < flipper.size(); i++) {
                View view = getLayoutInflater().inflate(R.layout.item_home_flipper, null);
                TextView tvTitle = view.findViewById(R.id.tvTitle);
                tvTitle.setText(flipper.get(i));
                titleFlipper.addView(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, SearchRecordActivity.class);
                        mContext.startActivity(intent);
                    }
                });
            }
            for (int i = 0; i < flipper2.size(); i++) {
                View view = getLayoutInflater().inflate(R.layout.item_home_flipper, null);
                TextView tvTitle = view.findViewById(R.id.tvTitle);
                tvTitle.setText(flipper2.get(i));
                titleFlipper2.addView(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, SearchRecordActivity.class);
                        mContext.startActivity(intent);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取通知权限
    private void getNOTIFICATION() {
        if (!NotificationManagerCompat.from(mContext)
                .areNotificationsEnabled()) {
            new NoticeDialog(mContext, new ShareDialogListener() {
                @Override
                public void onClick() {
                    XXPermissions.with(mContext)
                            .permission(Permission.NOTIFICATION_SERVICE)
                            .request(new OnPermissionCallback() {
                                @Override
                                public void onGranted(List<String> permissions, boolean all) {
                                }
                            });
                }
            }).show();
        }

    }


    @Override
    public void onStart() {
        super.onStart();
        if (homePagePresenter == null) {
            return;
        }
        if (netNum == 0) {
            //第一次延迟300ms请求个人信息
            //定时器 倒计时
            Observable.timer(300, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) {
                            homePagePresenter.getMineInfo();
                        }
                    });
        } else {
            homePagePresenter.getMineInfo();
        }
        netNum++;
        homePagePresenter.getCount();
        homePagePresenter.getMyWorkList();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getHashMap() != null) {
            Map<String, String> map = messageEvent.getHashMap();
        } else {
            String msg = messageEvent.getMessage();
            if (Utils.isNull(msg)) {
                return;
            }
            if ("refreshAccount".equals(msg)) {
                //更新账户了
                if (homePagePresenter == null) {
                    return;
                }
                pageNum = 1;
                isEnd = false;
                FileUtilss.deleteWaterMarkBg(mContext, "watermark.jpg");
                FileUtilss.deleteWaterMarkBg(mContext, "watermarkBlack.jpg");
                netNum = 0;
                homePagePresenter.checkLogin();
                homePagePresenter.getNewsList(pageNum);
                homePagePresenter.getAllBanner();
                homePagePresenter.getNotReadMessageByEmployee();
                //水印只有启动和切换账号才获取
                homePagePresenter.getWatermarkList();
                //ossKey切换账号才获取
                homePagePresenter.getOSSkey();
                //获取导航切换账号才获取
                homePagePresenter.getNavigations();

                //切换账号获取各公司的计费规则
                homePagePresenter.getCostConfig();
            } else if ("scrollToNews".equals(msg)) {
                consecutiveScrollerLayout.scrollToChild(llBanner);
            }
        }
    }

    @Override
    public void initImmersionBar() {
        if (getActivity() == null) {
            return;
        }
        ImmersionBar.with(getActivity())
                .transparentStatusBar()  /*透明状态栏，不写默认透明色*/  /*状态栏字体是深色，不写默认为亮色*/
                .statusBarDarkFont(false)
                .flymeOSStatusBarFontColor(R.color.white)  /*修改flyme OS状态栏字体颜色*/
                .keyboardEnable(false).navigationBarEnable(false)
                .init();
    }

    private boolean isShow = false;

    @OnClick({R.id.llSearchConent, R.id.llSearch2, R.id.llQrCode, R.id.llQrCode2, R.id.llLevel, R.id.tvNextLevel2, R.id.rlProgressBar, R.id.ivHead, R.id.viewFlipper, R.id.tvBindTaskLook, R.id.tvTodayTaskLook, R.id.tvInterviewTaskLook, R.id.tvJobLook})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.llSearch2:
            case R.id.llSearchConent:
                intent = new Intent(mContext, SearchRecordActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.llQrCode:
            case R.id.llQrCode2:
                new IntentIntegrator(getActivity())
                        .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)// 扫码的类型,可选：一维码，二维码，一/二维码
                        .setPrompt("请对准二维码")// 设置提示语
                        .setCameraId(0)// 选择摄像头,可使用前置或者后置
                        .setBeepEnabled(true)// 是否开启声音,扫完码之后会"哔"的一声
                        .setBarcodeImageEnabled(false)// 扫完码之后生成二维码的图片
                        .setOrientationLocked(false)//扫描方向固定
                        .setCaptureActivity(ScanActivity.class) // 设置自定义的activity是CustomActivity
                        .initiateScan();// 初始化扫码
                break;
            case R.id.ivHead:
                intent = new Intent(mContext, UserInfosActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.llLevel:
                intent = new Intent(mContext, WebCommonActivity.class);
                intent.putExtra("url", NavigationTools.getAllUrl(mContext, Urls.getCapacityAnalysisGrowth) + "&employeeCode=" + new ShareUtils().getString(mContext, StaticClass.EMPLOYEECODE, ""));
                intent.putExtra("title", "段位");
                startActivity(intent);
                break;
            case R.id.rlProgressBar:
            case R.id.tvNextLevel2:
                if (PermissionSpUtils.getBoolean(mContext,"08",false)) {
                    //有个人能力权限才可点击
                    intent = new Intent(mContext, WebCommonActivity.class);
                    intent.putExtra("url", NavigationTools.getAllUrl(mContext, Urls.getCapacityAnalysisDetail) + "&employeeCode=" + new ShareUtils().getString(mContext, StaticClass.EMPLOYEECODE, ""));
                    intent.putExtra("title", "明细");
                    startActivity(intent);
                }
                break;
            case R.id.viewFlipper:
                break;
            case R.id.tvTodayTaskLook:
                String code1 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_1_CODE, "");
                String code3 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_3_CODE, "");
                if ("04".equals(code1) || "04".equals(code3)) {
                    Map<String, String> map = new HashMap<>();
                    map.put("HomeBottom", "Xuexijihua");
                    map.put("code", "04");
                    EventBus.getDefault().post(new MyEvent(map));
                } else {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 4);
                    mContext.startActivity(intent);
                }
                break;
            case R.id.tvBindTaskLook:
                intent = new Intent(mContext, WxBindActivity.class);
                intent.putExtra("url", Urls.bing_wx);
                intent.putExtra("title", "我的绑定");
                mContext.startActivity(intent);
                break;
            case R.id.tvInterviewTaskLook:
                intent = new Intent(mContext, InterviewRecordsActivity.class);
                intent.putExtra("type", 2);
                mContext.startActivity(intent);
                break;
            case R.id.tvJobLook:
                intent = new Intent(mContext, WorksActivity.class);
                mContext.startActivity(intent);
                break;
        }
    }

    @Override
    public void loadMineInfo(MineInfoEntity data) {
        if (data != null) {
            if (tvName == null) {
                return;
            }
            GlideUtils.loadAvatar(mContext, data.getHeadPhotoThumbUrl(), ivHead);
            GlideUtils.loadAvatar(mContext, data.getHeadPhotoThumbUrl(), ivHead2);
            tvName.setText(data.getNickName());
            if (mContext.getPackageName().equals("com.zhaofengnum")) {
                tvCompany.setText(mContext.getResources().getString(R.string.app_name));
            }else{
                tvCompany.setText(new ShareUtils().getString(mContext, StaticClass.COMPANYNAME, ""));
            }
            tvLevel.setText(data.getLvlName());
            String[] level = data.getLvlText().split("\\，");
            if (level.length > 1) {
                tvNextLevel2.setText(level[1]);
            } else {
                tvNextLevel2.setText("");
            }
            tvScore.setText(ClearMoreZeroUtil.subZeroAndDot(data.getEnergyNum()));
            if ("0".equals(data.getLvlFlag())) {
                tvNextLevel.setText(data.getNextLvlName());
                tvUpgradeScore.setText(",距离" + data.getNextLvlName() + "还剩" + ClearMoreZeroUtil.subZeroAndDot(data.getNextNeedEnergyNum()) + "能量");
                float progress = (float) (Utils.changeNumber(data.getLvlMaxNergyNum()) - Utils.changeNumber(data.getNextNeedEnergyNum())) / Utils.changeNumber(data.getLvlMaxNergyNum());
//                float progress = (float) 0.5;
                progressBar.setProgress((int) (progress * 100));
            } else {
                progressBar.setProgress(100);
                tvUpgradeScore.setText("");
                tvNextLevel.setText("");
            }
            if (!Utils.isNull(data.getLvlImage())) {
                String url = data.getLvlImage();
                GlideUtils.load(mContext, url, ivLevel, R.drawable.icon_wangzhe_23);
            }
            new ShareUtils().putString(mContext, StaticClass.APPACCOUNT, data.getAppAccount());
            new ShareUtils().putString(mContext, StaticClass.POST, data.getPost());
            new ShareUtils().putString(mContext, StaticClass.DEVICENAME, data.getBindingDeviceName());
            new ShareUtils().putString(mContext, StaticClass.ORIGINALURL, data.getHeadPhotoUrl());
            new ShareUtils().putString(mContext, StaticClass.USER_AVATAR_URL, data.getHeadPhotoThumbUrl());
            new ShareUtils().putString(mContext, StaticClass.USER_NICKNAME, data.getNickName());
            new ShareUtils().putString(mContext, StaticClass.BELG_ORG_CODE, data.getBelgOrgCode());
            new ShareUtils().putString(mContext, StaticClass.BELG_ORG_NAME, data.getBelgOrgName());
            new ShareUtils().putString(mContext, StaticClass.BELG_CATALOG_CODE, data.getBelgCatalogCode());
        }

    }

    @Override
    public void loadBanner(List<HomeBannerEntity> data) {
        if (llBanner == null) {
            return;
        }
        if (data != null) {
            if (data.size() > 0) {
                llBanner.setVisibility(View.VISIBLE);
                homeBanner.setAdapter(new BannerImageAdapter<HomeBannerEntity>(data) {
                    @Override
                    public void onBindView(BannerImageHolder holder, HomeBannerEntity data, int position, int size) {
                        //图片加载自己实现
                        GlideUtils.loadCustomRound(mContext, data.getBannerUrl(), (ImageView) holder.itemView, 6);
                        holder.imageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(mContext, WebCommonActivity.class);
                                intent.putExtra("url", data.getJumpUrl());
                                mContext.startActivity(intent);
                            }
                        });
                    }
                }).addBannerLifecycleObserver(this)
                        .setBannerRound(Utils.dip2px(mContext, 6))
                        .setIndicator(new CircleIndicator(mContext));
            } else {
                llBanner.setVisibility(View.GONE);
            }
        } else {
            llBanner.setVisibility(View.GONE);
        }
    }

    private List<UnReadMessageEntity> unReadMessageEntityList = new ArrayList<>();

    @Override
    public void loadNotReadMessage(List<UnReadMessageEntity> data) {
        if (viewFlipper == null) {
            llViewFlipper.setVisibility(View.GONE);
            return;
        }
        viewFlipper.removeAllViews();
        if (viewFlipper.isFlipping()) viewFlipper.stopFlipping();
        if (null != data && data.size() > 0) {
            unReadMessageEntityList.clear();
            llViewFlipper.setVisibility(View.VISIBLE);
        } else {
            llViewFlipper.setVisibility(View.GONE);
        }
        unReadMessageEntityList.addAll(data);
        if (unReadMessageEntityList.size() > 1) {
            viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.slide_bottom_in));
            viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.slide_up_out));
            viewFlipper.startFlipping();
        }
        try {
            for (int i = 0; i < unReadMessageEntityList.size(); i++) {
                View view = getLayoutInflater().inflate(R.layout.item_home_notice, null);
                TextView tvTime = view.findViewById(R.id.tvTime);
                TextView tvTitle = view.findViewById(R.id.tvTitle);
                tvTitle.setText(unReadMessageEntityList.get(i).getMessage_content());
                tvTime.setText(Utils.getAccurateDate(unReadMessageEntityList.get(i).getCreate_time()));
                viewFlipper.addView(view);
                int j = i;
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 消息通知类型(0:积分变更通知，1：粉丝关注通知，2：点赞通知，3：评论通知，4：系统消息通知
                        Intent intent = null;
                        if ("0".equals(unReadMessageEntityList.get(j).getMessage_type())) {
                            intent = new Intent(mContext, MyIntegralActivity.class);
                            intent.putExtra("messageType", 0);
                        } else if ("1".equals(unReadMessageEntityList.get(j).getMessage_type())) {
                            intent = new Intent(mContext, MyPraiseActivity.class);
                            intent.putExtra("messageType", 1);
                        } else if ("2".equals(unReadMessageEntityList.get(j).getMessage_type())) {
                            intent = new Intent(mContext, MyPraiseActivity.class);
                            intent.putExtra("messageType", 2);
                        } else if ("3".equals(unReadMessageEntityList.get(j).getMessage_type())) {
                            intent = new Intent(mContext, MyPraiseActivity.class);
                            intent.putExtra("messageType", 3);
                        } else if ("4".equals(unReadMessageEntityList.get(j).getMessage_type())) {
                            intent = new Intent(mContext, MyIntegralActivity.class);
                            intent.putExtra("messageType", 4);
                        }
                        if (intent != null) {
                            mContext.startActivity(intent);
                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void loadNewsList(List<NewsEntiry> data) {
        loadEnd();
        if (llNews == null) {
            return;
        }
        if (pageNum == 1) {
            newsList.clear();
            adapter.notifyDataSetChanged();
        }
        if (!"13503825592".equals(new ShareUtils().getString(mContext, StaticClass.USERNAME, ""))) {
            if (data != null && data.size() > 0) {
                int oldNum = newsList.size();
                newsList.addAll(data);
                adapter.notifyItemRangeChanged(oldNum, data.size());
//            recyclerView.setBackgroundResource(R.color.white);
                llNews.setVisibility(View.VISIBLE);
            } else {
                isEnd = true;
                if (newsList.size() == 0) {
//                llNews.setVisibility(View.GONE);
//                recyclerView.setBackgroundResource(R.color.main_bg);
                    adapter.setEmptyView(emptyView);
                }
            }
        } else {
            for (int i = 0; i < data.size(); i++) {
                if (!data.get(i).getNewsTitle().contains("企小竹")) {
                    newsList.add(data.get(i));
                }
            }
            if (data.size() > 0) {
                adapter.notifyDataSetChanged();
                llNews.setVisibility(View.VISIBLE);
            } else {
                isEnd = true;
                if (newsList.size() == 0) {
                    adapter.setEmptyView(emptyView);
                }
            }
        }
    }

    @Override
    public void loadNavigationsList(List<NavigationEntity> data) {
        if (rcvNavigation == null) {
            return;
        }
        if (data != null) {
            navigationEntityList.clear();
            List<NavigationEntity> newData = NavigationTools.getNavigation(mContext,data, 0);
            if (newData.size() > 8) {
                navigationEntityList.addAll(newData.subList(0, 8));
                navigationAdapter.setType(1);
            } else {
                navigationEntityList.addAll(newData);
                navigationAdapter.setType(0);
            }
            navigationAdapter.notifyDataSetChanged();
            rcvNavigation.setVisibility(View.VISIBLE);
        } else {
            if (navigationEntityList.size() == 0) {
                rcvNavigation.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void loadCount(String num) {
        int numInt = Utils.changeNumber(num);
        if (rcvNavigation != null) {
            navigationAdapter.setChoice(numInt);
        }
        Map<String, String> map = new HashMap<>();
        map.put("HomeBottom", "Gonggao");
        map.put("num", numInt + "");
        EventBus.getDefault().post(new MyEvent(map));
    }

    private int taskInt = 0, bindInt = 0, workInt = 0, mouthPlanInt = 0, dayPlanInt = 0, interviewInt = 0;

    @Override
    public void loadMyWorkList(String bindNum, String taskNum, String workNum, String mouthPlanNum, String dayPlanNum, String interviewNum) {
        taskInt = Utils.changeNumber(taskNum);
        bindInt = Utils.changeNumber(bindNum);
        workInt = Utils.changeNumber(workNum);
        mouthPlanInt = Utils.changeNumber(mouthPlanNum);
        dayPlanInt = Utils.changeNumber(dayPlanNum);
        interviewInt = Utils.changeNumber(interviewNum);
        //根据代办数据和权限展示布局
        if (llTask == null) {
            return;
        }
        if (taskInt == 0 && bindInt == 0 && workInt == 0 && mouthPlanInt == 0 && dayPlanInt == 0 && interviewInt == 0) {
            llTask.setVisibility(View.GONE);
            new ShareUtils().putBoolean(mContext, StaticClass.BIND_WX, true);
        } else {
            if (!"13503825592".equals(new ShareUtils().getString(mContext, StaticClass.USERNAME, ""))) {
                llTask.setVisibility(View.VISIBLE);
                if (bindInt > 0) {
                    new ShareUtils().putBoolean(mContext, StaticClass.BIND_WX, false);
                    long time = new ShareUtils().getLong(mContext, StaticClass.EMPLOYEECODE + "-" + StaticClass.BIND_WX_TIME, 0);
                    if (time == 0) {
                        llBindTask.setVisibility(View.VISIBLE);
                    } else {
                        int day = (int) ((time - System.currentTimeMillis()));
                        if (day > 1000 * 3600 * 24 * 7) {
                            llBindTask.setVisibility(View.VISIBLE);
                            new ShareUtils().putLong(mContext, StaticClass.EMPLOYEECODE + "-" + StaticClass.BIND_WX_TIME, 0);
                        } else {
                            llBindTask.setVisibility(View.GONE);
                        }
                    }
                } else {
                    llBindTask.setVisibility(View.GONE);
                    new ShareUtils().putBoolean(mContext, StaticClass.BIND_WX, true);
                }
            }else{
                llBindTask.setVisibility(View.GONE);
                new ShareUtils().putBoolean(mContext, StaticClass.BIND_WX, true);
            }

            if (workInt > 0) {
                tvJob.setText("我的待办(" + workInt + ")");
                if (PermissionSpUtils.getBoolean(mContext,"09",false)) {
                    llJob.setVisibility(View.VISIBLE);
                } else {
                    llJob.setVisibility(View.GONE);
                }
            } else {
                llJob.setVisibility(View.GONE);
            }
            if (interviewInt > 0) {
                tvInterviewTask.setText("面试评价(" + interviewInt + ")");
                if (PermissionSpUtils.getBoolean(mContext,"06",false)) {
                    llInterviewTask.setVisibility(View.VISIBLE);
                } else {
                    llInterviewTask.setVisibility(View.GONE);
                }
            } else {
                llInterviewTask.setVisibility(View.GONE);
            }
            if ((dayPlanInt + mouthPlanInt + taskInt) > 0) {
                tvTodayTask.setText("我的任务(" + (dayPlanInt + mouthPlanInt + taskInt) + ")");
                if (PermissionSpUtils.getBoolean(mContext,"04",false)) {
                    llTodayTask.setVisibility(View.VISIBLE);
                } else {
                    llTodayTask.setVisibility(View.GONE);
                }
            } else {
                llTodayTask.setVisibility(View.GONE);
            }
        }
    }

    private void loadEnd() {
        if (null != smrRf && smrRf.getState() == RefreshState.Refreshing)
            smrRf.finishRefresh(true);
        if (null != smrRf && smrRf.getState() == RefreshState.Loading)
            smrRf.finishLoadMore(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (unbinder != null) {
            unbinder.unbind();
        }

    }
}
